package com.clz.latte.ec.sign;

import android.app.Activity;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.text.TextUtils;
import android.util.Log;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.clz.latte.delegate.LatteDaleGate;
import com.clz.latte.ec.R;
import com.clz.latte.ec.R2;
import com.clz.latte.ec.entity.User;
import com.clz.latte.net.RestClient;
import com.clz.latte.net.callback.IError;
import com.clz.latte.net.callback.IFailure;
import com.clz.latte.net.callback.ISuccess;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/1/3.
 * 注册
 */

public class SignUpDelegate extends LatteDaleGate {
    @BindView(R2.id.edit_name)
    TextInputEditText mEditName;
    @BindView(R2.id.edit_email)
    TextInputEditText mEditEmail;
    @BindView(R2.id.edit_phone)
    TextInputEditText mEditPhone;
    @BindView(R2.id.edit_pwd)
    TextInputEditText mEditPwd;
    @BindView(R2.id.edit_re_pwd)
    TextInputEditText mEditRePwd;
    @BindView(R2.id.btn_sign_up)
    Button mBtnSignUp;
    @BindView(R2.id.tv_link_sign_in)
    TextView mTvLinkSignIn;
    private ISignListener mISignListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ISignListener) {
            mISignListener = (ISignListener) activity;
        }
    }

    @OnClick(R2.id.btn_sign_up)
    void onClickSignUp() {
        if (checkForm()) {
            RestClient.builder()
                    .url("http://192.168.1.107:82/RestServer/data/user_profile.json")
                    .params("name", mEditName.getText().toString())
                    .params("email", mEditEmail.getText().toString())
                    .params("phone", mEditPhone.getText().toString())
                    .sucess(new ISuccess() {
                        @Override
                        public void onSucess(String response) {
                            Log.i("login",response);
                            Gson gson = new Gson();
                            final User user = gson.fromJson(response, User.class);
                            SignHandler.onSignUp(user,mISignListener);
                        }
                    })
                    .error(new IError() {
                        @Override
                        public void onError(int code, String message) {

                        }
                    })
                    .failure(new IFailure() {
                        @Override
                        public void onFailure() {

                        }
                    })
                    .build().post();
        }
    }

    @OnClick(R2.id.tv_link_sign_in)
    void onLinkSignUpClicked() {
        start(new SignInDelegate(), SINGLETASK);
    }

    @Override
    public Object setLayOut() {
        return R.layout.delegate_sign_up;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mTvLinkSignIn.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        mTvLinkSignIn.getPaint().setAntiAlias(true);//抗锯齿
    }

    /**
     * 检测注册信息合法性
     *
     * @return
     */
    private boolean checkForm() {
        boolean isPass = true;
        final String name = mEditName.getText().toString().trim();
        final String email = mEditEmail.getText().toString().trim();
        final String phone = mEditPhone.getText().toString().trim();
        final String pwd = mEditPwd.getText().toString().trim();
        final String rePwd = mEditRePwd.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            mEditName.setError("请输入姓名");
            isPass = false;
        } else {
            mEditName.setError(null);
        }
        if (email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            mEditEmail.setError("错误的邮箱格式");
            isPass = false;
        } else {
            mEditEmail.setError(null);
        }
        if (phone.isEmpty() || phone.length() != 11) {
            mEditPhone.setError("手机号格式不正确");
            isPass = false;
        } else {
            mEditPhone.setError(null);
        }
        if (pwd.isEmpty() || pwd.length() < 6) {
            mEditPwd.setError("密码长度不能小于6位");
            isPass = false;
        } else {
            mEditPwd.setError(null);
        }
        if (rePwd.isEmpty() || (!pwd.equals(rePwd))) {
            mEditRePwd.setError("两次输入密码不一致");
            isPass = false;
        } else {
            mEditRePwd.setError(null);
        }
        return isPass;
    }
}
